import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import request from "./axios/axios";
import Element, {Message} from 'element-ui'
import "element-ui/lib/theme-chalk/index.css"
import md5 from  'js-md5'
import * as echarts from 'echarts';
import 'mavon-editor/dist/css/index.css'
import mavonEditor from 'mavon-editor'
//需要挂载到Vue原型上


Vue.prototype.$axios = request
Vue.prototype.$md5= md5
Vue.prototype.$echarts = echarts
Vue.config.productionTip = false
Vue.use(Element)
Vue.use(mavonEditor)


require("./mock.js")
/* eslint-disable no-new */

//前端拦截器
router.beforeEach((to,from,next)=>{
  if (to.meta.requireAuth) {
    if (store.state.token!==null&&store.state.token!=='') {
      initMenu(store)
      request.get(`/user/authority/id=${store.state.id}`).then(res=>{
        if(store.state.authority!==res.data.data){
          Message({
            message:'权限错误，登录已失效',
            type:'warning'
          })
          store.commit('clear')
          next({
            path: '/login',
            query: {redirect: to.fullPath}
          })
        }
        else next()
      }).catch()

    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
})

//初始化动态列表
const initMenu=(store)=>{
  if (store.state.menuList.length > 0) {
    return ;
  }
  store.commit('initMenu')
}

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',

})
